/*******************************************************************************
 * Copyright (c) 2008-2010 Cedric Vidal
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Cedric Vidal - original idea, initial contribution and API
 *******************************************************************************/
package org.trans4j;

import net.sf.cglib.proxy.Enhancer;

import org.trans4j.cglib.Trans4jNamingPolicy;
import org.trans4j.impl.CachedMapper;
import org.trans4j.impl.PolymorphicMapper;

/**
 * @author <a href="mailto:c.vidal@proxiad.com">Cedric Vidal, ProxiAD Group</a>
 * 
 */
public class Trans4j {

	private Trans4j() {
		super();
	}

	public static <A> A mapper(Class<? extends A> clazz) {
		return mapper(clazz, null);
	}

	@SuppressWarnings("unchecked")
	public static <A> A mapper(Class<? extends A> clazz, ClassLoader classLoader) {
		CachedMapper cached = new CachedMapper();
		PolymorphicMapper polymorphic = new PolymorphicMapper();

		polymorphic.setDelegate(cached);

		Enhancer e = new Enhancer();
		e.setNamingPolicy(Trans4jNamingPolicy.INSTANCE);
		e.setSuperclass(clazz);
		e.setCallback(polymorphic);
		e.setClassLoader(classLoader);
		Object bean = e.create();
		return (A) bean;
	}

}
